package com.bdqn;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public class Jd2 {
    public static void main(String[] args) {
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db=dbf.newDocumentBuilder();
            Document document=db.parse("ch02/Short/src/Student.xml");
            NodeList nodeList=document.getElementsByTagName("Student");
            for (int i=0;i<nodeList.getLength();i++){
                    Element element= (Element) nodeList.item(i);
                    String id=element.getAttribute("id");
                    System.out.println("编号:"+id);
                    NodeList nodeList1=element.getChildNodes();
                    for (int j=0;j<nodeList1.getLength();j++){
                        if (nodeList1.item(j).getNodeType()== Node.ELEMENT_NODE){
                            Element element1= (Element) nodeList1.item(j);
                            System.out.println(element1.getTextContent());
                        }
                    }
                System.out.println("\n");
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
